package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.ParameterConfigure;
import com.originskyseed.platform.user.model.ParameterConfigureRelateLog;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IParameterConfigureRelateLogService;
import com.originskyseed.platform.user.service.IParameterConfigureService;
import com.originskyseed.platform.user.vo.request.ParameterConfigureRequestVO;

/**
 * 参数配置Controller类（OSI_PARAMETER_CONFIGURE）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/parameterConfigure")
@BaseLocaleResource(localeFileName = "i18n_user")
public class ParameterConfigureController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ParameterConfigureController.class);

	@DubboReference
	private IParameterConfigureService parameterConfigureService;

	@DubboReference
	private IParameterConfigureRelateLogService parameterConfigureRelateLogService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/parameterConfigureList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<ParameterConfigure>> parameterConfigureList(HttpServletRequest request) {
		BaseResponseBody<BasePage<ParameterConfigure>> result = null;
		try {
			// 请求对象
			ParameterConfigureRequestVO requestVO = this.getParameterBean(ParameterConfigureRequestVO.class);
			if(StringUtils.isBlank(requestVO.getParentId())){
				requestVO.setFirstLevel(1);
			}

			// 分页查询
			BasePage<ParameterConfigure> page = new BasePage<ParameterConfigure>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.parameterConfigureService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<ParameterConfigure>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<ParameterConfigure>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/parameterConfigureSave.json", method = RequestMethod.POST)
	public BaseResponseBody<ParameterConfigure> parameterConfigureSave(HttpServletRequest request) {
		BaseResponseBody<ParameterConfigure> result = null;
		try {
			// 获取当前登录用户
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			// 请求对象
			ParameterConfigure parameterConfigure = this.getParameterBean(ParameterConfigure.class);
			if(StringUtils.isBlank(parameterConfigure.getParentId())){
				parameterConfigure.setParentId(null);
			}
			if (this.parameterConfigureService.existCode(parameterConfigure)) {
				return new BaseResponseBody<ParameterConfigure>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.CODE_EXIST_ERROR.getMsg()), null);
			}
			if (StringUtils.isNotBlank(parameterConfigure.getId())) {
				// 记录日志
				ParameterConfigure oldParameterConfigure = this.parameterConfigureService.selectByPrimaryKey(parameterConfigure.getId());
				if (StringUtils.isNotBlank(parameterConfigure.getValue()) && !parameterConfigure.getValue().equals(oldParameterConfigure.getValue())) {
					ParameterConfigureRelateLog parameterConfigureRelateLog = new ParameterConfigureRelateLog();
					parameterConfigureRelateLog.setCode(parameterConfigure.getCode());
					parameterConfigureRelateLog.setName(parameterConfigure.getName());
					parameterConfigureRelateLog.setCreator(userInfo.getName());
					parameterConfigureRelateLog.setCreatorId(userInfo.getId());
					parameterConfigureRelateLog.setValue(parameterConfigure.getValue());
					parameterConfigureRelateLog.setOldValue(oldParameterConfigure.getValue());
					this.parameterConfigureRelateLogService.insertSelective(parameterConfigureRelateLog);
				}
				this.parameterConfigureService.updateByPrimaryKey(parameterConfigure);
			} else {
				this.parameterConfigureService.insertSelective(parameterConfigure);
			}
			result = new BaseResponseBody<ParameterConfigure>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), parameterConfigure);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<ParameterConfigure>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/parameterConfigureDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> parameterConfigureDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.parameterConfigureService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectParameterConfigureById.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody selectParameterConfigureById(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			ParameterConfigure parameterConfigure = this.parameterConfigureService.selectByPrimaryKey(id);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), parameterConfigure);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 列表查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectParameterConfigureList.json", method = RequestMethod.POST)
	public BaseResponseBody selectParameterConfigureList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			ParameterConfigure parameterConfigure = this.getParameterBean(ParameterConfigure.class);

			// 列表查询
			List<ParameterConfigure> parameterConfigureList = this.parameterConfigureService.selectList(parameterConfigure);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), parameterConfigureList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询参数配置树接口
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/parameterConfigureTree.json", method = RequestMethod.POST)
	public BaseResponseBody parameterConfigureTree(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			JSONArray jsonArray = this.parameterConfigureService.selectParameterConfigureTree();
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), jsonArray);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 通过父级code查询参数配置
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectByParentCode.json")
	public BaseResponseBody selectByParentCode(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			String code = this.getParameterString("code", null);

			List<ParameterConfigure> parameterConfigureList = null;
			ParameterConfigure parameterConfigure = this.parameterConfigureService.selectByCode(code);
			if (parameterConfigure != null) {
				ParameterConfigure query = new ParameterConfigure();
				query.setParentId(parameterConfigure.getId());
				parameterConfigureList = this.parameterConfigureService.selectList(query);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), parameterConfigureList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}